package com.example.todolist.repository

import android.app.Application
import android.util.Log
import com.example.todolist.database.DAO.CategoryDao
import com.example.todolist.database.Mydatabase
import com.example.todolist.database.model.CategoryEntity
import com.example.todolist.entity.CategoryDetail
import com.example.todolist.util.LoggerTag
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject


class CategoryRepository @Inject constructor(val categoryDao: CategoryDao?) {
    //    val categoryVOList: LiveData<List<CategoryVO>>=categoryDao.

//    companion object {
//        @JvmStatic
//        private var instance: CategoryRepository? = null
//
//        // 通过Application获取Dao实例
//        fun getInstance(application: Application): CategoryRepository {
//            return instance ?: synchronized(this) {
//                instance ?: CategoryRepository(
//                    // 从Room数据库获取Dao
//                    Mydatabase.getInstance(application)?.getCategoryDao()
//                ).also { instance = it }
//            }
//        }
//    }

    suspend fun insertCategory(categoryEntity: CategoryEntity) = withContext(Dispatchers.IO) {
        categoryDao?.insert(categoryEntity)
    }

    suspend fun queryDetails(): List<CategoryDetail> = withContext(Dispatchers.IO) {
        categoryDao?.queryDetail() ?: emptyList()
    }

    suspend fun deleteCategories(vararg categories: CategoryEntity) = withContext(Dispatchers.IO) {
        categoryDao?.delete(*categories)
        Log.d(LoggerTag.MAIN_CATEGORY, "deleteCategories: 批量删除完成....")
    }

    suspend fun queryCategories() = withContext(Dispatchers.IO) {
        categoryDao?.query()
    }

    suspend fun getAllTodoCnt() = withContext(Dispatchers.IO) {
        categoryDao?.getAllToDoCnt()
    }

    suspend fun getUnCateTodoCnt() = withContext(Dispatchers.IO) {
        categoryDao?.getUnCateTodoCnt()
    }

    suspend fun getUnCategory() = withContext(Dispatchers.IO) {
        categoryDao?.getUnCategory() ?: CategoryEntity()
    }
}